<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline head-form">
          <el-form-item v-if="btnFunc.some(btn=>btn=='add')">
            <el-button @click="bannerInfo()" size="small" icon="el-icon-plus" type="primary">添加栏目
            </el-button>

          </el-form-item>
        </el-form>
      <el-table stripe ref="table"  v-loading="tableLoading" :data="memberList" border row-key="id"
        @selection-change="handleSelectionChange" >
        <el-table-column show-overflow-tooltip type="selection" align="center" />
        <el-table-column show-overflow-tooltip prop="sort" label="排序" align="center"  width="80px"/>
        <el-table-column prop="coverUrl" label="图标" align="center" width="350px" >
          <template slot-scope="{row}">
              <div class="bannerImg" >
              <img :src="row.coverUrl||'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png'" />
              </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="type" label="类型" align="center">
          <template slot-scope="{row}">
            <div>
              <el-tag v-if="row.type == 1" type="warning" size="small">课程链接</el-tag>
              <el-tag v-if="row.type == 2" type="success" size="small">其他</el-tag>
            </div>
          </template>
        </el-table-column>




        <el-table-column show-overflow-tooltip prop="status" label="状态" align="center" >
          <template slot-scope="{row}">
            {{ row.status === 1 ? '正常' : '禁用' }}
            <el-switch v-if="!isAdmin||btnFunc.some(btn=>btn=='status')" v-model="row.status" :active-value="1"
              :inactive-value="0" size="small" @change="setStatus(row)" />
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="updateTime" label="修改时间" align="center" />

         <el-table-column v-if="isAdmin||(btnFunc.filter(value=>value=='edit'||value=='delete').length>0)" label="操作"
          width="120" align="center" fixed="right">
          <template slot-scope="scoped">
            <el-row>
              <el-tooltip v-if="isAdmin||btnFunc.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                placement="bottom">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="bannerInfo(scoped.row)" />
              </el-tooltip>
              <el-tooltip v-if="!isAdmin||btnFunc.some(btn=>btn=='delete')" class="item" effect="dark" content="删除"
                placement="bottom">
                <el-button :loading="delBtnLoading" type="danger" size="mini" class="el-icon-delete" @click="memberDel(scoped.row)" />
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="page-current">
        <el-pagination background :current-page="currentPage" :page-size="pageSize" prev-text="上一页" next-text="下一页"
          layout="total, prev, pager, next, jumper" style="display: inline-block;" :total="count" @current-change="handlePageChange" />
      </div>
    </el-tabs>

    <!-- 编辑表单 -->
   <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" :title="dialogTitle+'栏目'">
      <editForm ref="editForm" :infoForm="infoForm"  style="width: 100%" @submit="submit" @closeWin="closeWin"></editForm>
    </el-dialog>
  </div>
</template>
<script>
  import gzhApi from '@/api/gzh/gzh'
  import editForm from './edit'

  export default {
    name: "member",
    data() {
      return {
        tableLoading: false,
        // 当前页
        currentPage: 1,
        // 总条数
        count: 0,
        // 每页条数
        pageSize: 10,
        // 显示编辑角色弹框
        addBtnLoading: false,
        addDialogVisible: false,
        delBtnLoading: false,
        channelList: [],
        searchParam: {
          phone: '',
          channel: '',
          id: ''
        },
        status: [{
          value: 1,
          label: '正常'
        }, {
          value: 2,
          label: '禁用'
        }],
        memberList: [],
        deviceList: [],
        infoForm: {
          name: '',
          status: 1,
          device: 5,
          source: 2,
        },
        loading: false,
        tableHeight: document.body.clientHeight - 245,
        btnFunc: [],
        isAdmin: false,
        dialogTitle: "添加",
      }
    },
    components: {
      editForm
    },
    created() {
      const {
        btnFunc,
        isAdmin
      } = this.$store.state.user;
      console.log(this.$store.state.user)
      this.btnFunc = btnFunc
      this.isAdmin = isAdmin
      this.pageSize =10
    },
    mounted() {
      // 开启加载动画
      this.tableLoading = true
      // 获取数据列表
      this.getMemberList()
    },
    watch: {},
    methods: {
       handleClick(tab, event) {
              console.log(tab, event);
      },
      // 赠送vip
      giveVip(data) {
        this.$confirm('是否确认为该用户开通 至尊终身VIP会员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          console.log(data);
          gzhApi.giveVip({
            uid: data.uid,
            type: 4,
            nickName: data.username
          }).then(res => {
            this.getMemberList()
            console.log(res)
            this.$message({
              type: 'success',
              message: '赠送成功!'
            });
          }).catch(e => {
            this.$message({
              type: 'success',
              message: '赠送失败!'
            });
          })
        }).catch(() => {
          this.$message({
              type: 'success',
              message: '赠送失败!'
            });
        });
      },
      // 获取编辑数据
      bannerInfo(data){
        // 初始化表单
        this.initInfoForm()

        // 设置标题
        if (data == null) {
          this.dialogTitle = "添加";
          this.addDialogVisible=true
        } else {
        this.dialogTitle = "编辑";
        }
        this.$nextTick(function () {
          try {
            console.log(1111);
            gzhApi.bannerInfo({id: data.id}).then(res => {
              this.infoForm = res.data
              console.log( this.infoForm)
               // 显示弹窗
               this.addDialogVisible=true
              this.$refs.editForm.$refs['infoForm'].resetFields()

            }).catch(e => {
            })
          } catch (e) {

          }
        })
      },
      paramInput(data) {
        this.searchParam[data];
        for (var key in this.searchParam) {
          if (key != data) {
            this.searchParam[key] = '';
          }
        }

      },
      handleSelectionChange(row) {
        console.log(row)
      },
      batchDel() {
        console.log(this.$refs.table.clearSelection());
      },
      // 异步获取数据
      async queryData() {
        // 获取设备类型
        await this.getParamList()
      },
      // 初始化搜索条件
      initSearchParam() {
        this.searchParam = {
          phone: '',
          channel: '',
          id: ''
        }
      },
      getChannelList() {
        var that = this;
        gzhApi.talkchannel({
          pageIndex: 1,
          pageSize: 1000
        }).then(res => {

          console.log('渠道=', res)
          if (res.code == 200) {
            that.channelList = res.data.records
          }

        }).catch(e => {

        })
      },
      // 获取数据列表
      getMemberList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        searchParam.pageIndex = this.currentPage
        searchParam.pageSize = this.pageSize
        gzhApi.bannerList(searchParam).then(res => {
          this.memberList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.memberList = []
          this.tableLoading = false
        })
      },
      // 初始化表单
      initInfoForm() {
        this.infoForm = {
          type:0,
          name:'',
          productId:'',
          linkUrl:'',
          coverUrl:''
        }
      },
      // 删除记录
      memberDel(data) {
        this.$confirm('您确定要删该栏目？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          gzhApi.bannerDel({
            id: data.id
          }).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            this.delBtnLoading = false
            this.tableLoading = true
            this.getMemberList()
          }).catch(e => {
            this.delBtnLoading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 获取参数
      getParamList() {
        gzhApi.getParamList().then(res => {
          const {
            device_list
          } = res.data
          this.deviceList = device_list ? device_list : {}
        }).catch(e => {
          this.deviceList = {}
        })
      },
      // 设置状态
      setStatus(data) {
        console.log(data)
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status,
            id: data.id
          }
          this.tableLoading = true
          gzhApi.bannerEdit(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getMemberList()
          }).catch(e => {
            let memberList = JSON.parse(JSON.stringify(this.memberList))
            for (let i = 0; i < memberList.length; i++) {
              if (memberList[i].id == data.id) {
                memberList[i].status = data.status == 1 ? 0 : 1
              }
            }
            this.memberList = Object.assign([], memberList)
            this.tableLoading = false
          })
        }).catch(() => {
          let memberList = JSON.parse(JSON.stringify(this.memberList))
          for (let i = 0; i < memberList.length; i++) {
            if (memberList[i].id == data.id) {
              memberList[i].status = data.status == 1 ? 0 : 1
            }
          }
          this.memberList = Object.assign([], memberList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        });
      },
      handlePageChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getMemberList()
      },
      // 搜索
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getMemberList()
      },
      // 提交表单后逻辑处理
      submit(type) {
        if (type == 0) {
          // 添加
          this.search()
        } else {
          // 编辑
          this.tableLoading = true
          this.getMemberList()
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.addDialogVisible = false
        }
      },
      // 关闭窗体
      closeWin() {
        console.log("关闭")
        // 调用初始化表单方法
        this.initInfoForm()
        // 设置弹窗不可见
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .app-container {
    width: 100%;

    .page-current {
      text-align: center;
      padding: 15px 5px;
      padding-bottom: 0px;

      .sure-page-btn {
        display: inline-block;
        height: 28px;
        line-height: 0;
        width: 46px;
        padding: 0;
        vertical-align: top;
      }
    }
  }
  .bannerImg img{
    width: 100%;
  }
</style>
